﻿using System;

namespace Eflong.Monitor
{
    /// <summary>
    /// 预警值计算类
    /// </summary>
    public class EarlyWarningValue
    {
        /// <summary>
        /// 设置预警统计
        /// </summary>
        /// <param name="wV">预警值</param>
        /// <param name="wT">预警时间</param>
        /// <param name="wW">风量</param>
        public EarlyWarningValue( float wHV , float wLV , float wT , float wW , Record5Min r5min )
        {
            warningHightValue = wHV;
            warningLowValue = wLV;
            warningTime = wT;
            volumn = wW;
            record5Min = r5min;
            if ( warningTime < 0 )
            {
                warningTime = 1;
            }
            else if ( warningTime > 10 )
            {
                warningTime = 10;
            }
            Init( );
        }

        public void Init( )
        {
            warning = false;

            begin = DateTime.Now;
        }

        /// <summary>
        /// 判断是否预警
        /// </summary>
        /// <returns></returns>
        public bool IsEarlyWarning( )
        {
            if ( record5Min != null )
            {
                int iv;
                if ( volumn == 1 )
                {
                    iv = ( int )( record5Min.statValue.avg * 100 + 0.499 );
                }
                else
                {
                    iv = ( int )( ( record5Min.statValue.avg / 100.0 * volumn ) * 100 );
                }
                float fv = ( float )( iv / 100.0 );
                avgValue = fv;
                currValue = String.Format( "{0:F2}" , ( float )( iv / 100.0 ) ); //(float)(iv / 100.0);
                DateTime t = DateTime.Now;

                // 判断是否超过预警高值或低于预警低值

                if ( fv > warningHightValue || fv < warningLowValue )
                {
                    if ( !warning )
                    {
                        warning = true;
                        warningBegin = t;
                        maxValue = fv;
                        minValue = fv;
                        tmMax = t;
                        tmMin = t;
                    }
                    else
                    {
                        if ( maxValue < fv )
                        {
                            maxValue = fv;
                            tmMax = t;
                        }
                        if ( minValue > fv )
                        {
                            minValue = fv;
                            tmMin = t;
                        }
                        if ( t.Subtract( warningBegin ).TotalMinutes >= warningTime )
                        {
                            return true;
                        }
                    }
                    return false;
                }
                else
                {
                    if ( warning )
                    {
                        warningEnd = t;
                        warning = false;
                    }

                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 风量
        /// </summary>
        public float volumn;

        /*
         *  预警值
         */

        //public float warningValue;

        /// <summary>
        /// 预警低值

        /// </summary>
        public float warningLowValue;

        /// <summary>
        /// 预警高值

        /// </summary>
        public float warningHightValue;

        /// <summary>
        /// 延迟时间,按分钟为单位
        /// </summary>
        public float warningTime;

        /// <summary>
        /// 关联的5分钟统计
        /// </summary>
        public Record5Min record5Min;

        /// <summary>
        ///  预警
        /// </summary>
        private bool warning;

        /// <summary>
        /// 当前值
        /// </summary>
        public string currValue;

        /// <summary>
        /// 开始时刻

        /// </summary>
        private DateTime begin;

        /// <summary>
        /// 报警开始时刻

        /// </summary>
        private DateTime warningBegin;

        /// <summary>
        /// 报警结束时刻
        /// </summary>
        private DateTime warningEnd;

        /// <summary>
        /// 平均值
        /// </summary>
        public float avgValue;

        /// <summary>
        /// 最大值
        /// </summary>
        public float maxValue;

        /// <summary>
        /// 最小值
        /// </summary>
        public float minValue;

        /// <summary>
        /// 最大值时刻
        /// </summary>
        public DateTime tmMax;

        /// <summary>
        /// 最小值时刻
        /// </summary>
        public DateTime tmMin;
    }
}